package com.zxz.mq.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxz.mq.dto.TransMessageDto;
import com.zxz.mq.entity.TransMessage;
import com.zxz.mq.enums.TransMessageType;
import com.zxz.mq.service.IMessageSendService;
import com.zxz.mq.service.ITransMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@ConditionalOnProperty(name = "my.mq.mysql.enable",havingValue = "true")
@Service
public class MysqlMessageSendService implements IMessageSendService {

    @Value("${spring.application.name}")
    private String serviceName;

    @Autowired
    private ITransMessageService transMessageService;

    @Override
    public TransMessageDto messageSendReady(String exchange, String routingKey, String body) {
        String messageId = UUID.randomUUID().toString();
        TransMessageDto transMessageDto = new TransMessageDto();
        transMessageDto.setId(messageId);
        transMessageDto.setService(serviceName);
        transMessageDto.setExchange(exchange);
        transMessageDto.setRoutingKey(routingKey);
        transMessageDto.setPayload(body);
        transMessageDto.setSequence(0);
        transMessageDto.setDate(LocalDateTime.now());
        transMessageDto.setType(TransMessageType.SEND.name());

        TransMessage transMessage = new TransMessage();
        BeanUtils.copyProperties(transMessageDto,transMessage);
        transMessageService.save(transMessage);
        return transMessageDto;
    }

    @Override
    public void messageSendSuccess(String id) {
        transMessageService.removeById(id);
    }

    @Override
    public TransMessageDto messageSendReturn(String id, String exchange, String routingKey, String body) {
        return messageSendReady(exchange, routingKey, body);
    }

    @Override
    public void messageResend(String id) {
        TransMessage transMessage = transMessageService.getById(id);
        transMessage.setSequence(transMessage.getSequence() + 1);
        transMessageService.save(transMessage);
    }

    @Override
    public void messageDead(String id) {
        //todo 告警
        TransMessage transMessage = transMessageService.getById(id);
        transMessage.setType(TransMessageType.DEAD.name());
        transMessageService.save(transMessage);
    }

    @Override
    public List<TransMessageDto> messageSendReadyList() {
        List<TransMessage> transMessageList = transMessageService.list(
                Wrappers.<TransMessage>lambdaQuery()
                        .eq(TransMessage::getService, serviceName)
                        .eq(TransMessage::getType, TransMessageType.SEND.name())
                        //半小时前消费失败的消息
                        .lt(TransMessage::getDate, LocalDateTime.now().plusMinutes(-30))
        );
        if(CollectionUtils.isEmpty(transMessageList)){
            return new ArrayList<>();
        }

        List<TransMessageDto> transMessageDtos = new ArrayList<>(transMessageList.size());
        transMessageList.forEach(item ->{
            TransMessageDto dto = new TransMessageDto();
            BeanUtils.copyProperties(item,dto);
            transMessageDtos.add(dto);
        });
        return transMessageDtos;
    }
}
